<-- back

Longest Substring Without Repeating Characters

Link

Just have an array to keep track of all the previous letters we hit. After we reach a previously hit letter, simply start over with a new position.

public class Solution { public int lengthOfLongestSubstring(String s) { int result = 0; boolean[] hit = new boolean[256]; char[] c = s.toCharArray(); int incre = 0; for(int i=0; i< c.length; i++){ for(int j=i; j< c.length; j++){ if(hit[c[j]]){ result = Math.max(result, incre); incre = 0; Arrays.fill(hit,false); break; } else{ hit[c[j]]=true; incre++; } } } result = Math.max(result, incre); return result; } }